package com.lingxu;

import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

/**
 * @program: ptmh
 * @ClassName TestApi
 * @description:
 * @author: gxy
 * @create: 2025-07-15 09:54
 * @Version 1.0
 **/
public class ApiT {
    public static void main(String[] args) {
        try {
            // 基础URL
            String baseUrl = "http://localhost:8080/api/users/";

            // 生成随机参数
            Random random = new Random();
            int userId = random.nextInt(100); // 路径中的id
            int pageNum = random.nextInt(100);
            int pageSize = random.nextInt(100);
            int paramId = random.nextInt(100); // 参数中的id

            // 构建完整URL
            String urlString = baseUrl + userId +
                    "?pageNum=" + pageNum +
                    "&pageSize=" + pageSize +
                    "&id=" + paramId;

            // 创建连接
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应
            int responseCode = connection.getResponseCode();
            System.out.println("请求URL: " + urlString);
            System.out.println("响应码: " + responseCode);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应内容
            System.out.println("响应内容: " + response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


